const Router = require('@koa/router');
const { checkAuth, checkPermission } = require('../middlewares/auth');
const mapServiceController = require('../controllers/mapService.controller');

const router = new Router({ prefix: '/api/v1/map-services' });

/**
 * @swagger
 * /api/v1/map-services:
 *   post:
 *     tags: [地图服务]
 *     summary: 创建地图服务
 *     description: 创建新的地图服务配置
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - provider
 *               - apiKey
 *             properties:
 *               provider:
 *                 type: string
 *                 enum: [tencent, amap, tianditu]
 *                 description: 地图服务提供商
 *                 example: "tencent"
 *               apiKey:
 *                 type: string
 *                 description: 服务密钥
 *                 example: "1234567890abcdef"
 *               config:
 *                 type: object
 *                 description: 服务配置
 *                 example: {
 *                   "webKey": "web1234567890",
 *                   "mobileKey": "mob1234567890",
 *                   "secretKey": "sec1234567890"
 *                 }
 *               usageQuota:
 *                 type: integer
 *                 description: 使用配额
 *                 example: 100000
 *               expiresAt:
 *                 type: string
 *                 format: date-time
 *                 description: 过期时间
 *                 example: "2024-12-31T23:59:59Z"
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "map_001"
 *                 message:
 *                   type: string
 *                   example: "地图服务创建成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.post('/', checkAuth, checkPermission('map_service:create'), mapServiceController.createMapService);

/**
 * @swagger
 * /api/v1/map-services:
 *   get:
 *     tags: [地图服务]
 *     summary: 获取地图服务列表
 *     description: 获取所有地图服务配置
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: provider
 *         schema:
 *           type: string
 *           enum: [tencent, amap, tianditu]
 *         description: 服务提供商筛选
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [active, inactive]
 *         description: 状态筛选
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 total:
 *                   type: integer
 *                   example: 10
 *                 pages:
 *                   type: integer
 *                   example: 1
 *                 current:
 *                   type: integer
 *                   example: 1
 *                 services:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                         example: "map_001"
 *                       provider:
 *                         type: string
 *                         example: "tencent"
 *                       status:
 *                         type: string
 *                         example: "active"
 *                       usageCount:
 *                         type: integer
 *                         example: 5000
 *                       usageQuota:
 *                         type: integer
 *                         example: 100000
 *                       expiresAt:
 *                         type: string
 *                         format: date-time
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/', checkAuth, checkPermission('map_service:read'), mapServiceController.getMapServices);

/**
 * @swagger
 * /api/v1/map-services/{id}:
 *   get:
 *     tags: [地图服务]
 *     summary: 获取服务详情
 *     description: 获取指定地图服务的详细配置信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 服务ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "map_001"
 *                 provider:
 *                   type: string
 *                   example: "tencent"
 *                 config:
 *                   type: object
 *                   example: {
 *                     "webKey": "web1234567890",
 *                     "mobileKey": "mob1234567890"
 *                   }
 *                 usageCount:
 *                   type: integer
 *                   example: 5000
 *                 usageQuota:
 *                   type: integer
 *                   example: 100000
 *                 expiresAt:
 *                   type: string
 *                   format: date-time
 *                 status:
 *                   type: string
 *                   example: "active"
 *                 usageStats:
 *                   type: object
 *                   properties:
 *                     daily:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           date:
 *                             type: string
 *                             format: date
 *                             example: "2024-01-01"
 *                           count:
 *                             type: integer
 *                             example: 100
 *                     monthly:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           month:
 *                             type: string
 *                             example: "2024-01"
 *                           count:
 *                             type: integer
 *                             example: 3000
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 服务不存在
 */
router.get('/:id', checkAuth, checkPermission('map_service:read'), mapServiceController.getMapService);

/**
 * @swagger
 * /api/v1/map-services/{id}:
 *   put:
 *     tags: [地图服务]
 *     summary: 更新地图服务
 *     description: 更新指定地图服务的配置信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 服务ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               apiKey:
 *                 type: string
 *                 description: 服务密钥
 *                 example: "1234567890abcdef"
 *               config:
 *                 type: object
 *                 description: 服务配置
 *                 example: {
 *                   "webKey": "web1234567890",
 *                   "mobileKey": "mob1234567890"
 *                 }
 *               usageQuota:
 *                 type: integer
 *                 description: 使用配额
 *                 example: 100000
 *               expiresAt:
 *                 type: string
 *                 format: date-time
 *                 description: 过期时间
 *                 example: "2024-12-31T23:59:59Z"
 *               status:
 *                 type: string
 *                 enum: [active, inactive]
 *                 description: 服务状态
 *                 example: "active"
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "地图服务更新成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 服务不存在
 */
router.put('/:id', checkAuth, checkPermission('map_service:update'), mapServiceController.updateMapService);

/**
 * @swagger
 * /api/v1/map-services/{id}:
 *   delete:
 *     tags: [地图服务]
 *     summary: 删除地图服务
 *     description: 删除指定的地图服务配置
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 服务ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "地图服务删除成功"
 *       400:
 *         description: 无法删除（正在使用中）
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 服务不存在
 */
router.delete('/:id', checkAuth, checkPermission('map_service:delete'), mapServiceController.deleteMapService);

/**
 * @swagger
 * /api/v1/map-services/{id}/status:
 *   patch:
 *     tags: [地图服务]
 *     summary: 更新服务状态
 *     description: 更新指定地图服务的状态
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 服务ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - status
 *             properties:
 *               status:
 *                 type: string
 *                 enum: [active, inactive]
 *                 description: 新的状态
 *                 example: "active"
 *     responses:
 *       200:
 *         description: 状态更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "地图服务状态更新成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 服务不存在
 */
router.patch('/:id/status', checkAuth, checkPermission('map_service:update'), mapServiceController.updateMapServiceStatus);

/**
 * @swagger
 * /api/v1/map-services/{id}/usage/increment:
 *   post:
 *     tags: [地图服务]
 *     summary: 增加使用次数
 *     description: 增加指定地图服务的使用计数
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 服务ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               count:
 *                 type: integer
 *                 description: 增加的次数（默认为1）
 *                 example: 1
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 currentCount:
 *                   type: integer
 *                   example: 5001
 *                 remainingQuota:
 *                   type: integer
 *                   example: 94999
 *       400:
 *         description: 超出使用配额
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 服务不存在
 */
router.post('/:id/usage/increment', checkAuth, checkPermission('map_service:update'), mapServiceController.incrementUsageCount);

/**
 * @swagger
 * /api/v1/map-services/{id}/usage/reset:
 *   post:
 *     tags: [地图服务]
 *     summary: 重置使用次数
 *     description: 重置指定地图服务的使用计数
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 服务ID
 *     responses:
 *       200:
 *         description: 重置成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "使用次数已重置"
 *                 previousCount:
 *                   type: integer
 *                   example: 5000
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 服务不存在
 */
router.post('/:id/usage/reset', checkAuth, checkPermission('map_service:update'), mapServiceController.resetUsageCount);

module.exports = router; 